
#include "stm32f10x.h"
#include "sys.h"
#include "delay.h"

#define SRC_LED1_OFF GPIO_SetBits  (GPIOC,GPIO_Pin_13);
#define SRC_LED1_ON  GPIO_ResetBits(GPIOC,GPIO_Pin_13);

#define DP_Common_Left GPIO_Pin_7
#define DP_Common_Right GPIO_Pin_8

/*
宏定义，数码管a-g各段对应的比特，更换硬件只用改动以下8行
*/
#define    a 0x04   //  AAAA
#define    b 0x01   // F    B
#define    c 0x10   // F    B
#define    d 0x40   //  GGGG
#define    e 0x80   // E    C
#define    f 0x02   // E    C
#define    g 0x08   //  DDDD    H
#define    h 0x20
           

/*
自动生成段码表
*/
unsigned char LED_digit_table[]={
a+b+c+d+e+f,    //"0"
b+c,            //"1"
a+b+d+e+g,      //"2"
a+b+c+d+g,      //"3"
b+c+f+g,        //"4"
a+c+d+f+g,      //"5"
a+c+d+e+f+g,    //"6"
a+b+c,          //"7"
a+b+c+d+e+f+g,  //"8"
a+b+c+d+f+g,    //"9"
b+c+e+f+g,      //"H"
a+f+e+g,        //"C"
d+e+f,          //"L"
g,              //"-"
0x00            //black
};

#undef a
#undef b
#undef c
#undef d
#undef e
#undef f
#undef g

u8 _digCode[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};

void ViewA(u8 num)
{
    GPIO_WriteBit(GPIOB, DP_Common_Left, Bit_RESET);
    GPIO_Write(GPIOA, _digCode[num]);
    Delay_Ms(6);
    GPIO_WriteBit(GPIOB, DP_Common_Left, Bit_SET);
}

void ViewB(u8 num)
{
    GPIO_WriteBit(GPIOB, DP_Common_Right, Bit_RESET);
    GPIO_Write(GPIOA, _digCode[num]);
    Delay_Ms(6);
    GPIO_WriteBit(GPIOB, DP_Common_Right, Bit_SET);
}

int main(void)
{
    u8 i;
    GPIO_InitTypeDef def;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    Delay_Init();

    def.GPIO_Pin = GPIO_Pin_13;
    def.GPIO_Mode = GPIO_Mode_Out_PP;
    def.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC, &def);

    def.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
    def.GPIO_Mode = GPIO_Mode_Out_PP;
    def.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &def);

    def.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    def.GPIO_Mode = GPIO_Mode_Out_PP;	//推挽输出
    def.GPIO_Speed = GPIO_Speed_50MHz;	//IO口速度为50MHz
    GPIO_Init(GPIOA, &def);

    for(i = 0; i < 5; i++)
    {
        SRC_LED1_ON;
        Delay_Ms(60);
        SRC_LED1_OFF;
        Delay_Ms(60);
    }

    GPIO_WriteBit(GPIOB, DP_Common_Left, Bit_SET);
    GPIO_Write(GPIOA, _digCode[0]);Delay_Ms(200);
    GPIO_Write(GPIOA, _digCode[1]);Delay_Ms(200);
    GPIO_Write(GPIOA, _digCode[2]);Delay_Ms(200);
    GPIO_Write(GPIOA, _digCode[3]);Delay_Ms(200);
    GPIO_Write(GPIOA, _digCode[8]);Delay_Ms(200);
    
    /* Infinite loop 
    while (1)
    {
        for (u8 i = 0; i <= 99; i++)
        {
            for (u8 j = 0; j < 30; j++)
            {
                u8 g;
                if (i >= 10)
                {
                    ViewA(i / 10);
                    g = i - (i / 10) * 10;
                }
                else
                {
                    ViewA(0);
                    g = i;
                }
                ViewB(g);
            }
        }
    }*/
}


#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}
#endif
